package com.yuoj.yuojbackendjudgeservice.codesandbox.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.yuoj.backendcommon.codesandbox.ExecuteCodeRequest;
import com.yuoj.backendcommon.codesandbox.ExecuteCodeResponse;
import com.yuoj.backendcommon.common.ErrorCode;
import com.yuoj.backendcommon.exception.BusinessException;
import com.yuoj.yuojbackendjudgeservice.codesandbox.CodeSandBox;


/**
 * @author 刘渠好
 * @since 2025-01-15 22:14
 * 远程代码沙箱
 */
public class RemoteCodeSandBox implements CodeSandBox {

    //加密头和秘钥
    public static final String AUTH_REQUEST_HEADER = "auth";

    public static final String AUTH_REQUEST_SECRET="secretKey";
    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest requestParam) {
        System.out.println ("调用远程代码沙箱!");
        //调用远程实现的代码沙箱
        String url="http://localhost:8090/executeCode";
        String param= JSONUtil.toJsonStr (requestParam);
        String body = HttpUtil.createPost (url)
                .header (AUTH_REQUEST_HEADER, AUTH_REQUEST_SECRET)
                .body (param)
                .execute ()
                .body ();
        if (StrUtil.isBlank (body)){
            throw new BusinessException (ErrorCode.API_REQUEST_ERROR,"executeCode remoteSandbox error,message"+body);
        }
        return JSONUtil.toBean (body, ExecuteCodeResponse.class);

    }
}
